<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="所属物业" prop="propertyId">-->
<!--          <el-select v-model="queryParams.propertyId" placeholder="请选择物业" style="width:120px" size="mini" filterable  @change="selectEstate">-->
<!--            <el-option v-for="property in propertyListOptions"-->
<!--                       :key="property.id"-->
<!--                       :label="property.propertyName"-->
<!--                       :value="property.id"/>-->
<!--          </el-select>-->
<!--      </el-form-item>-->

<!--      <el-form-item label="所属小区" prop="estateId">-->
<!--        <el-select v-model="queryParams.estateId" placeholder="请选择小区"  filterable clearable style="width: 150px;" size="mini">-->
<!--          <el-option v-for="estate in estateListOptions"-->
<!--                     :key="estate.id"-->
<!--                     :label="estate.estateName"-->
<!--                     :value="estate.id"/>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="所属商家" prop="merId">
        <el-select v-model="queryParams.merId" placeholder="请选择所属商家" filterable  size="mini">
          <el-option v-for="shop in shopListOptions"
                     :key="shop.id"
                     :label="shop.shopName"
                     :value="shop.id"/>
        </el-select>
      </el-form-item>

      <el-form-item label="结算状态" prop="settlementState">
        <el-select v-model="queryParams.settlementState" placeholder="请选择结算状态" clearable size="mini" style="width: 130px;">
          <el-option :value="0" label="未结算"/>
          <el-option :value="1" label="已结算"/>
        </el-select>
      </el-form-item>



      <el-form-item label="核销时间" prop="dateRange">
        <el-date-picker v-model="dateRange"  clearable size="mini" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport"
          v-hasPermi="['module:productorder:export']">物业总数据导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExportMerTotal"
                   v-hasPermi="['module:productorder:export']">商家总数据导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExportMerDetail"
                   v-hasPermi="['module:productorder:export']">商家明细导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-download" size="mini" :disabled="multiple" :loading="settleLoading" @click="handleSettlementOrder"
                   v-hasPermi="['module:productorder:export']">确认结算</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column label="订单号" align="center" prop="orderNo" width="160"/>
<!--      <el-table-column label="所属物业" align="center" prop="propertyName" width="150"/>-->
<!--      <el-table-column label="所属小区" align="center" prop="estateName" width="150"/>-->
      <el-table-column label="所属商家" align="center" prop="shopName" width="200"/>
      <el-table-column label="用户姓名" align="center" prop="realName" />
      <el-table-column label="订单总价" align="center" prop="totalPrice">
        <template slot-scope="scope">{{scope.row.totalPrice}}元</template>
      </el-table-column>
      <el-table-column label="支付金额" align="center">
        <template slot-scope="scope">{{scope.row.payPrice}}元</template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="createTime" width="160"/>
      <el-table-column label="支付时间" align="center" prop="payTime" width="160"/>
      <el-table-column label="订单状态" align="center" prop="status" fixed="right">
        <template slot-scope="scope">
          <div>
            <!-- 订单状态 0 - 未支付   1 - 待支付  2 - 已支付  3 - 已核销 4 - 已评价
            5 - 订单取消  6 - 已申请退货  7 - 同意退货  8 - 退货完成 9 - 拒绝退货 10 - 已完成 -->
            <el-tag v-if="scope.row.status === 0" style="cursor: pointer" :type="'info'">未支付</el-tag>
            <el-tag v-if="scope.row.status === 1" style="cursor: pointer" :type="''">待支付</el-tag>
            <el-tag v-if="scope.row.status === 2" style="cursor: pointer" :type="'success'">已支付</el-tag>
            <el-tag v-if="scope.row.status === 3" style="cursor: pointer" :type="''">已核销</el-tag>
            <el-tag v-if="scope.row.status === 4" style="cursor: pointer" :type="'success'">已评价</el-tag>
            <el-tag v-if="scope.row.status === 5" style="cursor: pointer" :type="''">订单取消</el-tag>
            <el-tag v-if="scope.row.status === 6" style="cursor: pointer" :type="''">已申请退货</el-tag>
            <el-tag v-if="scope.row.status === 7" style="cursor: pointer" :type="''">同意退货</el-tag>
            <el-tag v-if="scope.row.status === 8" style="cursor: pointer" :type="''">退货完成</el-tag>
            <el-tag v-if="scope.row.status === 9" style="cursor: pointer" :type="''">拒绝退货</el-tag>
            <el-tag v-if="scope.row.status === 10" style="cursor: pointer" :type="'success'">已完成</el-tag>
          </div>
        </template>
      </el-table-column >

      <el-table-column label="结算状态" align="center" prop="settlementState" fixed="right">
        <template slot-scope="scope">
          <div>
            <el-tag v-if="scope.row.settlementState === 0" style="cursor: pointer" :type="'warning'">未结算</el-tag>
            <el-tag v-if="scope.row.settlementState === 1" style="cursor: pointer" :type="'success'">已结算</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-s-goods">
            <router-link :to="'/orders/info?orderId=' + scope.row.id+'&from=order'" class="link-type">
              订单详情
            </router-link>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"/>
  </div>
</template>

<script>
import {
  listOrder,
  getOrder,
  delOrder,
  addOrder,
  updateOrder,
  exportOrderTotal,
  exportOrderMerTotal,
  exportOrderMerDetail,
  settlementOrder
} from "@/api/module/product/order";

import { listNoPage,  } from "@/api/module/mer/mer";
import { listPropertyNoPage } from "@/api/module/property/property";
import { listAllEstate } from "@/api/module/estate/estate";

export default {
  name: "Order",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 结算遮罩层
      settleLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      dateRange:[],
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      shopListOptions:[],
      // 小区列表格数据
      estateListOptions: [],
      //物业公司列表
      propertyListOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status:2,
        orderType:0,
        merId:undefined,
        orderNo: undefined,
        beginDate:undefined,
        endDate:undefined,
        settlementState: undefined,
      },
      queryParamsEstate: {
        propertyId:undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: { }
    };
  },
  created() {
    this.getList();
    this.getPropertyList();
    this.getEstateList();
    this.getMerList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getMerList() {
      listNoPage({isShowProduct:0}).then(response => {
        this.shopListOptions = response.data;
      });
    },
    getPropertyList() {
      listPropertyNoPage().then(response => {
        this.propertyListOptions = response.data;
      });
    },
    selectEstate(val){
      this.queryParamsEstate.propertyId = val;
      this.getEstateList();
    },
    getEstateList() {
      listAllEstate(this.queryParamsEstate).then(response => {
        console.log('getEstateList',response.data);
        this.estateListOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        orderNo: undefined,
        outTradeNo: undefined,
        uid: undefined,
        realName: undefined,
        userPhone: undefined,
        userAddress: undefined,
        cartId: undefined,
        freightPrice: undefined,
        totalNum: undefined,
        totalPrice: undefined,
        totalPostage: undefined,
        payPrice: undefined,
        payPostage: undefined,
        deductionPrice: undefined,
        couponId: undefined,
        couponPrice: undefined,
        paid: undefined,
        payTime: undefined,
        payType: undefined,
        createTime: undefined,
        updateTime: undefined,
        status: 0,
        refundStatus: 0,
        refundReasonWapImg: undefined,
        refundReasonWapExplain: undefined,
        refundReasonTime: undefined,
        refundReasonWap: undefined,
        refundReason: undefined,
        refundPrice: undefined,
        deliverySn: undefined,
        deliveryName: undefined,
        deliveryType: undefined,
        deliveryId: undefined,
        gainIntegral: undefined,
        useIntegral: undefined,
        backIntegral: undefined,
        mark: undefined,
        isDel: undefined,
        unique: undefined,
        remark: undefined,
        merId: undefined,
        isMerCheck: undefined,
        combinationId: undefined,
        pinkId: undefined,
        cost: undefined,
        seckillId: undefined,
        bargainId: undefined,
        verifyCode: undefined,
        storeId: undefined,
        shippingType: undefined,
        isChannel: undefined,
        isRemind: undefined,
        isSystemDel: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      console.log('dateRange',this.dateRange);
      if(this.dateRange !=undefined && this.dateRange.length === 2){
        this.queryParams.beginDate = this.dateRange[0];
        this.queryParams.endDate = this.dateRange[1];
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length!==1;
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getOrder(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addOrder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除订单编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delOrder(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportOrderTotal(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExportMerTotal() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有订单数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportOrderMerTotal(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },

    /** 导出按钮操作 */
    handleExportMerDetail() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有订单数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportOrderMerDetail(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    /**
     * 结算订单
     */
    handleSettlementOrder(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认对选中订单进行结算?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.settleLoading = true;
        return settlementOrder(ids);
      }).then(() => {
        this.settleLoading = false;
        this.getList();
        this.msgSuccess("结算成功");
      }).catch(() => {});
    },
  }
};
</script>
